﻿using Qy.Common;
using Qy.Models;
using Qy.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Linq;
using System.Text.Json;

namespace Qy.Web.Authorize
{
    public class GlobalActionMonitor : ActionFilterAttribute
    {
        private readonly LoginUtil _LoginUtil;
        private readonly LogRepository _logRepository;
        public GlobalActionMonitor(LogRepository logRepository, LoginUtil loginUtil)
        {
            _logRepository = logRepository;
            _LoginUtil = loginUtil;
        }

        /// <summary>
        /// 日志记录
        /// OnResultExecuted是在Action中的代码执行之后运行的方法。
        /// OnActionExecuting在action method调用前发生。
        /// OnActionExecuted在action method调用后发生, 但是在result执行前发生 (在 view 呈现前)
        /// OnResultExecuting在result执行前发生(在view 呈现前)
        /// OnResultExecuted 在result执行后发生(在view 呈现后)
        /// </summary>
        /// <param name="context"></param>
        public override void OnResultExecuted(ResultExecutedContext context)
        {
            if (context.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) return;

            //获得注解信息，如果没有注解返回，不执行记录
            LogAttribute logAttribute = GetLogAttribute(controllerActionDescriptor);
            if (logAttribute == null) return;

            try
            {
                //string method = context.HttpContext.Request.Method.ToUpper(); //POST  GET  ...
                string jsonResult = string.Empty;
                //if (context.Result is ContentResult result && result.ContentType == "application/json")
                //{
                //    jsonResult = result.Content.Replace("\r\n", "").Trim();
                //    Console.WriteLine(jsonResult);
                //}
                if (context.Result is JsonResult result2)
                {
                    jsonResult = JsonSerializer.Serialize(result2.Value); 
                }

                UserAction user = _LoginUtil.UserAction();
                LogSql logSql = new()
                {
                    UserId = user.UserId,
                    Name = logAttribute.Name,
                    DoThing = jsonResult,
                    ControllerAction = user.Controller + "/" + user.Action,
                    Ip = user.Ip,
                    Time = DateTime.Now
                };
                _logRepository.InsertLog(logSql);

            }
            catch (Exception ex)
            {
                Console.WriteLine("记录操作日志出错了:" + ex.Message);
            }
        }

        private static LogAttribute GetLogAttribute(ControllerActionDescriptor controllerActionDescriptor)
        {
            var attribute = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true)
                .FirstOrDefault(a => a.GetType().Equals(typeof(LogAttribute)));

            return attribute as LogAttribute;
        }
    }
}
